package org.example.spring_book.config;

import lombok.extern.slf4j.Slf4j;
import org.example.spring_book.model.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@Slf4j
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {
    @ExceptionHandler
    public Object handler(Exception e) {
        log.error("发生异常, e: ",e);
        return Result.fail();
    }

    @ExceptionHandler
    public Object handler(ArrayIndexOutOfBoundsException e) {
        log.error("发生异常, e:{}: ",e.getMessage());
        return Result.fail();
    }
    @ExceptionHandler
    public Object handler(NullPointerException e) {
        log.error("发生异常, e: ",e);
        return Result.fail("发生NullPointerException 异常");
    }

    //图标异常
    public Object handler(NoResourceFoundException e) {
        log.error("发生异常, e: {},path:{}",e.getDetailMessageCode(),e.getResourcePath());
        return Result.fail("发生NoResourceFoundException 异常");
    }
}
